<?php
/**
 * alltosun.com 活动控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2014-9-15 下午2:52:28 $
 * $Id$
 */

class Action
{

    private $per_page = 20;

    /**
     * 活动列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_advenced   = Request::Get('is_advenced', 0);
        $search_filter = Request::Get('search_filter', array());

        $filter = array();
        $filter['status'] = $status;

        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter[' `title` LIKE '] = $search_filter['title'].'%';
        }

        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if(isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }

        $event_list = array();

        $event_list = get_data_list('event', $filter, ' ORDER BY `id` DESC ', $page, $per_page = 20);

        //处理活动的预览图片
        foreach ($event_list as $k=>$v) {
            $event_list[$k]['preview_pic'] = _image($v['preview_pic'], 'small');
        }

        Response::assign('event_list', $event_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);

        Response::display('admin/event_list.html');
    }

    /**
     * 活动添加
     */
    public function add()
    {
        Response::display('admin/add.html');
    }

    /**
     * 活动编辑
     */
    public function edit()
    {
        $id = Request::getParam('id', 0);
        if ($id) {
            $event_info = _uri('event', $id);
            Response::assign('event_info', $event_info);
        }

        Response::display('admin/edit.html');
    }

    /**
     * 创建和修改
     */
    public function create()
    {
        $id         = Request::post('id', 0);
        $event_info = Request::post('event', array());
        
        $event_info['preview_pic'] = '';
        
        if ($_FILES['preview_pic']['error'] == 0) {
            $image_info = upload_file($_FILES['preview_pic'], false, 'event');
            if ($image_info) {
                $event_info['preview_pic']   =  $image_info['file_path'];
            }
        }
        
        $event = array();
        if (!$id) {
            
            // 判断今天是否已经发布了活动
            if ('ok' != $this->is_event_send()) {
            	return array('一天只能发布一条活动!', 'error', AnUrl('event/admin'));
            }
            
            $message_id = message_center_helper::create_message($event_info['title'], 'event', @$event_info['preview_pic']);
            if ($message_id) {
                
                // 保存活动
                $event['msg_id'] = $message_id;
                $event['title'] = $event_info['title'];
                $event['preview_pic'] = @$event_info['preview_pic'];
                $event['content'] = $event_info['content'];
                $event['add_time'] = date('Y-m-d H:i:s');
                $event['add_date'] = date('Y-m-d');
                $event_id = _model('event')->create($event);

                if ($event_id) {
                    event_helper::push($message_id, $event_info['title']);
                } else {
                    return '活动保存失败!';
                }
            } else {
                return '消息保存失败!';
            }

        } else {
            
            $event['title'] = $event_info['title'];
            if ($event_info['preview_pic']) {
            	$event['preview_pic'] = $event_info['preview_pic'];
            }
            
            $event['content'] = $event_info['content'];
            // 更新活动表
            $res = _model('event')->update($id, $event);
            
            // 获取这条活动消息
            $cur_event = _uri('event', $id);
            
            $message = array();
            $message['title']       = $event_info['title'];
            $message['content']     = $event_info['content'];
            $message['preview_pic'] = '';
            if ($event_info['preview_pic']) {
            	$message['preview_pic'] = $event_info['preview_pic'];
            	message_center_helper::update_event_message($cur_event['msg_id'], $event_info['title'], $message['preview_pic']);
            } else {
                message_center_helper::update_event_message($cur_event['msg_id'], $event_info['title']);
            }
            
        }

        //添加或修改成功,执行跳转
        Response::assign('status', 1);
        Response::redirect('event/admin');

    }


    /**
     * 删除
     */
    public function delete()
    {
        $id = Request::getParam('id', 0);
        $message_info = _uri('event', $id);

        if (!$message_info) {
        	return '没有此活动信息!';
        }

        $msg_id = $message_info['msg_id'];

        // 修改状态
        $event_res = _model('event')->update($id, array('status'=>0));
        if ($event_res) {
            message_center_helper::update_message($id, array('status'=>0));
        }
        return 'ok';
    }

    /**
     * 还原
     */
    public function change_status()
    {

        // 接受id
        $id = Request::getParam('id', 0);

        //获取此活动消息信息
        $message_info = _uri('event', $id);
        if (!$message_info) {
            return '没有此活动信息!';
        }

        $msg_id = $message_info['msg_id'];
        //将event表中的状态进行修改
        $event_res = _model('event')->update($id, array('status'=>1));

        if ($event_res) {
            //将message_center中的信息进行状态修改
//             $message_res = _model('message_center')->update($id, array('status'=>1));
                message_center_helper::update_message($id, array('status'=>1));

        }
        Response::redirect('event/admin');

    }

    /**
     * 物理删除
     */
    public function delete_event()
    {

        //接收id
        $id = Request::getParam('id', 0);


        $event_info = _uri('event', $id);
        if(!$event_info){
            return '这个活动不存在!';
        }
        _model('event')->delete($id);
        _model('message_center')->delete($event_info['msg_id']);

        return 'ok';

    }

    /**
     * 判断当天是否已经发布了活动
     */
    public function is_event_send()
    {
        //获取当前的日期
        $cur_date = date('Y-m-d', time());
        $event_info = _model('event')->read(array('status'=>1, 'add_date'=>$cur_date));

        if ($event_info) {
        	echo 'no';
        } else {
            echo 'ok';
        }
    }



}

?>